package com.getword.web.controller;

import com.getword.domain.School;
import com.getword.domain.User;
import com.getword.service.UserService;
import com.getword.web.model.Result;
import com.getword.web.model.UserQueryCondition;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api/user")
public class ApiUserController {
    @Autowired
    private UserService service;

    @PostMapping
    public Result create(User user){
        return service.addUser(user);
    }

    @PutMapping
    public Result update(User user){
        return service.updateUser(user);
    }

    @GetMapping
    public Result query(){
        return service.getUsers();
    }
    // 分页查询
    @GetMapping("/query")
    public Result queryPage(int page, int size, User user, HttpServletRequest request){
        Pageable pageable = PageRequest.of(page-1, size);
        return service.getUserPage(pageable, user);
    }

    @GetMapping("/count")
    public Result count(User user){
        return service.getCount(user);
    }

    // 搜索用户
//    @GetMapping("/search")
//    public Result searchUser(String username, String loginName, Long schoolId, String role){
//        return null;
//    }

    // 修改密码
    @PostMapping("/updatePwd")
    public Result updatePwd(String oldPwd, String pwd){
        return service.updatePwd(oldPwd, pwd);
    }

    // 上传头像,教师、学生
    @PostMapping("/uploadHead")
    public Result uploadHead(MultipartFile file){
        return service.uploadHead(file);
    }

    // 通过cookie传入token
    @PostMapping("/upload/post")
    public Result uploadPost(MultipartFile file, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token = "";
        for(Cookie c : cookies){
            if(c.getName().equals("token")){
                token = c.getValue();
                break;
            }
        }
        if("".equals(token)){
            return Result.fail("没有权限");
        }
        return service.uploadImg(file, token);
    }

    // 获取当前登陆用户信息
    @GetMapping("/loginUser")
    public Result loginedUser(){
        return service.getLoginUser();
    }

    // 获取登陆用户的文章数量
    @GetMapping("/articleCountOfUser")
    public Result articleCountOfUser(Long userId){
        return service.getArticleCountOfUser(userId);
    }

    //{username, gender,birthday,school,intro}
    @PutMapping("/update")
    public Result updateUser(User user, Long school){
        if(school!=null){
            user.setSchool(new School(school));
        }else{
            user.setSchool(null);
        }
        return service.updateUser(user);
    }

    // checkLoginName
    @GetMapping("/checkLoginName")
    public Result checkLoginName(String loginName){
        return service.checkLoginName(loginName);
    }
}
